package cn.desk.index.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.osgi.interceptor.ServiceAware;

import cn.calm.commons.action.BaseAction;
import cn.calm.commons.provider.RequestProvider;

public class IndexAction<T> extends BaseAction<T> implements
		ServiceAware<RequestProvider> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(IndexAction.class);
	private List<RequestProvider> requestProviders;

	public String execute() throws Exception {
		log.debug("execute start");
		try {
			if (requestProviders != null) {
				for (RequestProvider p : requestProviders) {
					Map<String, Object> map = p.provider();
					for (Map.Entry<String, Object> en : map.entrySet()) {
						getRequest().setAttribute(en.getKey(), en.getValue());
					}
				}
			}
			return "success";
		} finally {
			log.debug("execute end");
		}
	}

	@Override
	public String getModeName() {
		return "index";
	}

	public List<RequestProvider> getRequestProviders() {
		return requestProviders;
	}

	public void setRequestProviders(List<RequestProvider> responseProviders) {
		this.requestProviders = responseProviders;

	}

	public void setRequestProviders(RequestProvider requestProvider) {
		if (requestProviders == null) {
			requestProviders = new ArrayList<RequestProvider>();
		}
		requestProviders.add(requestProvider);
	}

	@Override
	public Class<T> getEntityClass() {
		return null;
	}

	@Override
	public void setServices(List<RequestProvider> arg0) {
		requestProviders = arg0;
	}

}
